home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / StdLib / strtod.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  2KB  |  86 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* strtod.c - converts ascii to a double */
  18.  
  19. #include <stddef.h>
  20. #include <ctype.h>
  21.  
  22. extern double strtosd(char *, char **, double);
  23. extern double strtosud(char *, char **, double);
  24.  
  25. double strtod(string, ptr)
  26. char  *string;
  27. char **ptr;
  28. {
  29.     double retval = 0.0;
  30.     double sign = 1.0;
  31.     char *strptr = string;
  32.     char *scan_index = string;
  33.     int  exp = 0;
  34.     int  valid = 0;              /* Advances when a value is established */
  35.  
  36.     retval = strtosd(strptr, &scan_index, 10.0);
  37.     if (strptr != scan_index) {
  38.         valid++;
  39.         strptr = scan_index;
  40.         if (retval < 0.0) {
  41.             sign = -1.0;
  42.             retval *= -1.0;
  43.             }
  44.         }
  45.     else {
  46.         while (isspace(*strptr))
  47.             strptr++;
  48.         if (*strptr == '-') {
  49.             sign = -1.0;
  50.             strptr++;
  51.             }
  52.         }
  53.  
  54.     if (*strptr == '.')  {
  55.         strptr++;
  56.         retval += strtosud(strptr, &scan_index, .1);
  57.         if (strptr != scan_index) {
  58.             valid++;
  59.             strptr = scan_index;
  60.             }
  61.         }
  62.  
  63.     if ((valid > 0) && (tolower(*strptr) == 'e'))  {
  64.         strptr++;
  65.         exp = strtol(strptr, &scan_index, 10);
  66.         strptr = scan_index;
  67.         if (exp > 0)  {
  68.             while (exp-- > 0)
  69.                 retval *= 10.0;
  70.             }
  71.         else if (exp < 0)  {
  72.             while (exp++ < 0)
  73.                 retval /= 10.0;
  74.             }
  75.         }
  76.  
  77.     if (ptr != NULL) {
  78.         if (valid > 0)
  79.             *ptr = strptr;
  80.         else
  81.             *ptr = string;
  82.         }
  83.  
  84.     return(retval * sign);
  85. }
  86.